package gandalf.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

@Entity
public class Rezervacija implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date datum;
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.EAGER)
    private List<Postavka> postavka;
    @ManyToOne
    private Stranka stranka;


    /**
     * Get the value of stranka
     *
     * @return the value of stranka
     */
    public Stranka getStranka() {
        return stranka;
    }

    /**
     * Set the value of stranka
     *
     * @param stranka new value of stranka
     */
    public void setStranka(Stranka stranka) {
        this.stranka = stranka;
    }



    /**
     * Get the value of vstopnica
     *
     * @return the value of vstopnica
     */
    public List<Postavka> getPostavka() {
        return postavka;
    }

    /**
     * Set the value of vstopnica
     *
     * @param vstopnica new value of vstopnica
     */
    public void setPostavka(List<Postavka> postavka) {
        this.postavka = postavka;
    }


    /**
     * Get the value of datum
     *
     * @return the value of datum
     */
    public Date getDatum() {
        return datum;
    }

    /**
     * Set the value of datum
     *
     * @param datum new value of datum
     */
    public void setDatum(Date datum) {
        this.datum = datum;
    }



    /**
     * Get the value of id
     *
     * @return the value of id
     */
    public Long getId() {
        return id;
    }

    /**
     * Set the value of id
     *
     * @param id new value of id
     */
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "gandalf.model.Rezervacija[id=" + id + "]";
    }
    public boolean hasPostavka(Long idPostavke) {

        for(Postavka p : postavka) {
            if(p.getVstopnica().getId().equals(idPostavke)) {
                return true;
            }
        }

        return false;

    }

    public void updatePostavke(List<Postavka> postavke) {
        for(Postavka p : postavke) {
            if(hasPostavka(p.getVstopnica().getId())) {
                postavka.get(indexOfPostavkaWithVstopnica(p.getVstopnica().getId())).setKolicina(p.getKolicina());
            } else {
                postavka.add(p);
            }
        }
    }


    private int indexOfPostavkaWithVstopnica(Long idVstopnica) {
        int i = 0;
        for(Postavka p : postavka) {
            if(p.getVstopnica().getId().equals(idVstopnica)) {
                return i;
            }
            i++;
        }

        return -1;
    }
}
